package cn.progen.dao;

import com.sun.org.apache.xalan.internal.xsltc.compiler.SourceLoader;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.*;

/**
 * Created by wangYuDong on 2017/4/16.
 */
public class DButil {
    private Properties config = null;

    public DButil() {
       this.loadConfig();
    }

    public Connection connectDB() {
        Connection conn = null;
        try {
            Class.forName(config.getProperty("jdbc.driverClass"));
            System.out.println(config.getProperty("jdbc.jdbcUrl"));
            conn = DriverManager.getConnection(config.getProperty("jdbc.jdbcUrl"),
                    config.getProperty("jdbc.user"),
                    config.getProperty("jdbc.password"));
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
            return  null;
        } catch (Exception e) {
            e.printStackTrace();
            return  null;
        }
    }

    public List<Map<String,Object>> querySql(String sql){
        Connection conn=connectDB();
        try{
            Statement stmt = conn.createStatement();
            ResultSet resultSet = stmt.executeQuery(sql);
            return  resultSetToList(resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public List<Map<String,Object>> querySql(String sql,String... arges){
        Connection conn=connectDB();
        try{
            PreparedStatement pstmt = conn.prepareStatement(sql);
            for(int i=0;i<arges.length;i++){
                pstmt.setString(i+1,arges[i]);
            }
            ResultSet resultSet = pstmt.executeQuery();
            return  resultSetToList(resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public  List<Map<String,Object>> resultSetToList(ResultSet rs) throws SQLException {
        if (rs == null)
            return Collections.EMPTY_LIST;
        ResultSetMetaData md = rs.getMetaData(); //得到结果集(rs)的结构信息，比如字段数、字段名等
        int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数
        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
        Map<String,Object> rowData = new HashMap();
        while (rs.next()) {
            rowData = new HashMap(columnCount);
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnLabel(i), rs.getObject(i));
            }
            list.add(rowData);
        }
        return list;
    }


    public void loadConfig(){
        Properties p=new Properties();
        InputStream is=SourceLoader.class.getClassLoader().getSystemResourceAsStream("dbutil.properties");
        try {
            p.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.config=p;
    }

}
